﻿using ProjectCore.Application.AppUser.Dto;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ProjectCore.Application
{
    public class AppProductType : IAppProductType
    {
        private readonly IBaseRepository<ProductType> baseRepository;
        private readonly MyContext myContext;
        private readonly IProductTypeDomainService productType1;
        public AppProductType(IBaseRepository<ProductType> _baseRepository, MyContext _myContext, IProductTypeDomainService _productDomainService)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            productType1 = _productDomainService;
        }

        /// <summary>
        /// 回文
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ProductType> GetProductType(int Id)
        {
            return await productType1.GetProductType(Id);
        }


        /// <summary>
        /// 获取商品分类集合
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<List<PproductType>>> GetProductTypes(int Pid, int State, string Name, int pageIndex, int pageSize)
        {
            try
            {
                var fen = from a in myContext.Products
                          join b in myContext.ProductTypes
                          on a.TypeID equals b.ParentID into tp
                          from tt in tp.DefaultIfEmpty()
                          join c in myContext.PicUrls
                          on a.Photo equals c.ID into pi
                          from pp in pi.DefaultIfEmpty()
                          where (Pid == 0 || a.TypeID.Equals(Pid)) && (State == 0 || a.State.Equals(State)) && (Name == "" || a.ProName.Equals(Name))
                          select new PproductType
                          {
                              ProID = a.ProID,
                              Photo = pp.ID == 0 ? "" : pp.Photo,
                              ProName = tt.ProTypeName,
                              Sales = a.Sales,
                              Sort = a.Sort,
                              State = a.State,
                              CreateTime = a.CreateTime
                          };
                int d = fen.Count();
                fen = fen.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                HeaderResult<List<PproductType>> result = new HeaderResult<List<PproductType>>
                {
                    IsSucceed = true,
                    Result = await fen.ToListAsync(),
                    Total = d,
                };
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> IsDel(int Id)
        {
            return await productType1.IsDel(Id);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="productType"></param>
        /// <returns></returns>
        public async Task<int> Upt(ProductType productType)
        {
            return await productType1.Upt(productType);
        }
    }
}
